home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / cutils.arc / CONVERT.C next >
Text File  |  1985-08-30  |  2KB  |  64 lines

  1. /*
  2.                 AUTHOR          Jon Wesener
  3.                 DATE            7 / 19 / 85
  4.                 PURPOSE           These routines are used for conversions
  5.                                 between integers and strings.
  6. */
  7.  
  8. /*
  9.         ATOI converts a string of specified base to an integer
  10.          err= atoi(string, &number, base);
  11.         RETURNS:        0= Error        1= Success
  12. */
  13.  
  14. atoi(str,num,base)
  15. char    *str;
  16. unsigned     *num,base;
  17. {
  18.         int     digit;          /* holds value of ASCII converted character */
  19.  
  20.         *num= 0;
  21.         while (*str)
  22.         {
  23.                 if ( (*str >= '0') && (*str <= '9') )
  24.                         digit= *(str++) - '0';
  25.  
  26.                 else if ( (*str >= 'A') && (*str <= 'Z') )
  27.                         digit= *(str++) - 'A' + 10;
  28.  
  29.                 else if ( (*str >= 'a') && (*str <= 'z') )
  30.                         digit= *(str++) - 'a' + 10;
  31.  
  32.                 else return(0);         /* digit was unrecognized */
  33.  
  34.                 if (digit < base)
  35.                         *num= *num * base + digit;
  36.                 else return(0);         /* digit was invalid in base */
  37.         }
  38.         return(1);
  39. }
  40.  
  41. /*
  42.         ITOA converts an integer to a string of specified base
  43.          itoa(string, number, base);
  44.         RETURNS:        nothing
  45. */
  46.  
  47. itoa(str,num,base)
  48. char    *str;
  49. unsigned     num, base;
  50. {
  51.         int     digit;
  52.         char    temp[30], *ptr;
  53.  
  54.         ptr=temp + 29;
  55.         *ptr= '\0';
  56.         while (num)
  57.         {
  58.                 digit= num % base;      /* get last character */
  59.                 *(--ptr)=  (digit < 10)? digit+'0' :digit+'A'- 10;
  60.                 num= num / base;
  61.         }
  62.         strcpy(str,(*ptr)?ptr:"0");
  63. }
  64.